package com.jiamao.goplay.ui.main.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.google.android.material.appbar.AppBarLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.view.CustomCoordinatorLayout;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ConvertUtils;

/**
 * Created by goldze on 2018/7/18.
 *
 *  下拉滚动 appbar滚动 Toolbar显示
 */

public class TabBar6Fragment extends BaseFragment {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_6;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    private int mScrollY = 0;
    @Override
    public void initData() {
        super.initData();

       // ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.mIv));
        ImmersionBar.setTitleBar(this, binding.getRoot().findViewById(R.id.toolbar));

        //设置ToolBar
        //  ImmersionBar.titleBarMarginTop(this,  binding.getRoot().findViewById(R.id.toolbar));

        CustomCoordinatorLayout customCoordinatorLayout = binding.getRoot().findViewById(R.id.curr);
        customCoordinatorLayout.setmZoomView(binding.getRoot().findViewById(R.id.img));

        customCoordinatorLayout.setmMoveView(binding.getRoot().findViewById(R.id.layout),binding.getRoot().findViewById(R.id.ll));

         AppBarLayout appBarLayout = (AppBarLayout)  binding.getRoot().findViewById(R.id.app_bar);
       // swipeRefreshLayout.setEnableRefresh(false);
        Toolbar  linearLayout = binding.getRoot().findViewById(R.id.toolbar);
        appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {

            Log.d("HTML:"," VER:"+verticalOffset);

            if (verticalOffset < 0 ){
             //   swipeRefreshLayout.setEnableRefresh(false);
            }else {
              //  swipeRefreshLayout.setEnableRefresh(true);
            }


            int Offset = Math.abs(verticalOffset); //目的是将负数转换为绝对正数；

            linearLayout.setBackgroundColor(changeAlpha(getResources().getColor(R.color.orange)
                    , Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange()));

            /**
             * 当前最大高度便宜值除以2 在减去已偏移值 获取浮动 先显示在隐藏
             */
            if (Offset < appBarLayout.getTotalScrollRange() / 2) {

              //  linearLayout.setAlpha((appBarLayout.getTotalScrollRange() / 2 - Offset * 1.0f) / (appBarLayout.getTotalScrollRange() / 2));
                /**
                 * 从最低浮动开始渐显 当前 Offset就是  appBarLayout.getTotalScrollRange() / 2
                 * 所以 Offset - appBarLayout.getTotalScrollRange() / 2
                 */
            } else if (Offset > appBarLayout.getTotalScrollRange() / 2) {
                float floate = (Offset - appBarLayout.getTotalScrollRange() / 2) * 1.0f / (appBarLayout.getTotalScrollRange() / 2);
              //  linearLayout.setAlpha(floate);
            }

            //  ImmersionBar.with(this).statusBarDarkFont(Math.abs(i) >= offHeight, 0.2f).init();

        });


        // CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)  binding.getRoot().findViewById(R.id.toolbar_layout);
        NestedScrollView scrollView =  binding.getRoot().findViewById(R.id.item_detail_container);
        Button button = binding.getRoot().findViewById(R.id.btntext);
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = ConvertUtils.dp2px(80);
            int color = ContextCompat.getColor(getContext(), R.color.orange) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int[] location = new int[2];
                //  magicIndicator.getLocationOnScreen(location);
                //   int xPosition = location[0];
                //   int yPosition = location[1];
                Log.d("HTML","lastScrollY:"+lastScrollY+" H:"+h+" mScrollY:"+mScrollY+" oldScrollY:"+oldScrollY+" scrollY:"+scrollY);

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
                    linearLayout.setAlpha(1f * mScrollY / h);
                    linearLayout.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
                    // ivHeader.setTranslationY(mOffset - mScrollY);
                }else {
                    // linearLayout.setAlpha(0);
                    // linearLayout.setBackgroundColor(0);
                }
                if (scrollY == 0) {
                    button.setText("白色");
                    linearLayout.setAlpha(255);
                    linearLayout.setBackgroundColor(0);
                    linearLayout.setVisibility(View.GONE);
                    //   ivBack.setImageResource(R.drawable.back_white);
                    // ivMenu.setImageResource(R.drawable.icon_menu_white);
                } else {
                    linearLayout.setVisibility(View.VISIBLE);
                    button.setText("黑色");
                    linearLayout.setBackgroundColor(Color.WHITE);
                    //      ivBack.setImageResource(R.drawable.back_black);
                    //  ivMenu.setImageResource(R.drawable.icon_menu_black);
                }

                lastScrollY = scrollY;
            }
        });


//        collapsingToolbarLayout.post(() -> {
//
//
//
//            int offHeight = collapsingToolbarLayout.getHeight() - ImmersionBar.getStatusBarHeight(this);
//            appBarLayout.addOnOffsetChangedListener((appBarLayout1, i) -> {
//                ImmersionBar.with(this).statusBarDarkFont(Math.abs(i) >= offHeight, 0.2f).init();
//            });
//        });


        //ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.toolbar));
    }
    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, 0, 128, 0);
    }


}
